﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cs499.Database;

namespace cs499.Classes
{
    public partial class ListClasses : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["action"] != null) { 
                if(Request.QueryString["action"]=="success"){
                    ListActionMessage.Text = "Class Successfully added - ";
                }
            }
            else{
                ListActionMessage.Text="";
            }

            if (!HttpContext.Current.User.IsInRole("admin"))
            {
                GridView1.Columns[0].Visible = false;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton classesMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("ClassesMenuButton");
            classesMenuButton.CssClass = "selected";

            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = cs499.Database.ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                //EntityDataSource1.Where = "it.DepartmentId=" + (CurrentDepartment.Id).ToString();
                DepartmentLabel.Text = CurrentDepartment.Name;

            }
            else
            {
                DepartmentLabel.Text = "(No departments defined)";
            }
        }

        protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
        {

        }

        protected void exportclassbutton_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder exportFile = new System.Text.StringBuilder();

            //add header info
            exportFile.AppendLine("#FIELD INFO");
            exportFile.AppendLine("#Course Number: number assigned to the course (eg 101)");
            exportFile.AppendLine("#Course Title: title or description of the course (eg Intro to Computer Science)");
            exportFile.AppendLine("#Maximum Enrollment: maximum number of students that may enroll for the course (eg 30)");
            exportFile.AppendLine("#Number of Sections: number of sections of the course that will be offered (eg 1)");
            exportFile.AppendLine("#Place Requirements: semicolon delimited list of buildings and/or rooms where the course must be taught (eg SC; SC101) OPTIONAL");
            exportFile.AppendLine("");

            //add example lines
            exportFile.AppendLine("#EXAMPLES");
            exportFile.AppendLine("#499,SR PROJ:TEAM SOFTWARE DESIGN,40,1,TH;TH324");
            exportFile.AppendLine("#100,INTRO COMPUTERS & PROGRAM,32,1");
            exportFile.AppendLine("");
            exportFile.AppendLine("");

            //add field name line
            exportFile.AppendLine("#Course Number,Course Title,Maximum Enrollment,Number of Sections,Place Requirements");

            Model1Container database = new Model1Container();
            
            foreach (Course course in database.Set<Course>().Include("Department").Include("PlaceRequirements").Where(crtofilter=>crtofilter.Department.Name == Master.DepartmentSelection.SelectedValue ))
            {
                exportFile.AppendLine(course.Number.ToString() + "," +
                                      course.CourseTitle + "," +
                                      course.MaximumEnrollment.ToString() + "," +
                                      course.NumberOfSections.ToString() + "," +
                                      course.PlaceRequirements.PlaceListToString(";"));
            }

            Response.ContentType = @"application/x-msdownload";
            Response.AppendHeader("content-disposition", "attachment; filename=Courses.csv");

            Response.Write(exportFile.ToString());
            Response.Flush();
            Response.End();
        }
    }
}